<?php


namespace App\Service;

use App\Models\Common\China;
use App\Models\Constant\StudentConstant;
use App\Models\Student\Student;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

class StudentService extends CommonService {


    public function __construct($model) {
        $this->instance = new $model();
    }

    public function add(array $info) {
        $info['full_name'] = $info['xing'].$info['ming'];
        return parent::add($info); // TODO: Change the autogenerated stub
    }

    public function lst(array $conditions): Builder {
        $records = parent::lst($conditions); // TODO: Change the autogenerated stub


        $records = $records->with(['getCollege']);

        return $records;
    }

    public function detail($id) {
        $record =  $this->instance->where(compact('id'))
            ->with(['getCollege','getMajorClassify','getTestPostgraduateCollege',
                'getTestPostgraduateMajorClassify','getApplyPostgraduateMajor'])
        ->first();
        $record->prov_name = China::where('code',$record->hu_prov_code)->first()->name??'未知';
        $record->city_name = China::where('code',$record->hu_city_code)->first()->name??'未知';
        $record->url = asset($record->postgraduate_url);
        return $record;
    }

    public function result($name,$phone) {
        $status = true;
        $msg = "";
        $imgPath = "";
        $is_allow_edit = false;
        $student = Student::where(['full_name'=>$name,'phone'=>$phone])->first();

        if(empty($student)){
            $status = false;
            $msg = '未查到该学生信息';
            return compact('status','msg','imgPath','is_allow_edit','student');
        }

        //$id = $student->id;
        $id = $student->phone;
        $allCerts = scandir(public_path('/mail/cert'));

        foreach ($allCerts as $allCert) {
            if(Str::startsWith($allCert, $id)){
                $imgPath = asset('/mail/cert/'.$allCert);
            }
        }
        //$imgPath = asset('/common/image/school/1.png');
        $is_allow_edit = $student->status==StudentConstant::STATUS_CHECKING;
        if(empty($imgPath)){
            $status = false;
            $msg = '未查到证书信息';
            return compact('status','msg','imgPath','is_allow_edit','student');
        }

        return compact('status','msg','imgPath','is_allow_edit','student');

    }



}
